package cn.wolfcode.crm.util;


import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.domain.Permission;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Iterator;
import java.util.List;

/**
 * 菜单工具类
 */
public class MenuUtil {

    public static final String JSON_MENUS_ON_SESSION = "json_menus_on_session";

    public static void filtermenu(List<Menu> menus){
        Subject subject = SecurityUtils.getSubject();//获取当前登录对象
       /* subject.isPermitted()*/
        Iterator<Menu> iterator = menus.iterator();
        while(iterator.hasNext()){
            Menu next = iterator.next();
            //如果不为空,才需要权限
            Permission permission = next.getPermission();
            if(permission != null){
                //如果需要权限而他没有该权限则删除该列
                if(!subject.isPermitted(permission.getResources())){
                    iterator.remove();
                }
            }
            //如果有权限或不需要权杖,则检查其后辈是否需要权限
            filtermenu(next.getChildren());
        }
    }
}